<?php
/**
 * 主配置文件
 */

if (!defined('IN_SEXMALL'))
{
	die('Hacking attempt');
}


/* 加载项目配置文件 */
require(CONF_DIR . 'db.config.php');

/* 加载初始文件 */
require(ROOT_PATH . 'includes/init.php');



if (is_spider())
{
	/* 如果是蜘蛛的访问，那么默认为访客方式，并且不记录到日志中 */
	if (!defined('INIT_NO_USERS'))
	{
		define('INIT_NO_USERS', true);
	}
}

/* 用户自动登录 */
if (!defined('INIT_NO_USERS'))
{
	$user_token = get_user_token();

	$auth = wap_getcookie("WAP[auth]");


	if ($_GET['usertoken'] == '{usertoken}'){
		$_GET['username'] = '';
	}
	if ($_GET['usertoken'] == '{usertoken}' || strlen($_GET['usertoken']) != 32){
		$_GET['usertoken'] = null;
	}

	/* APP联合登录 */
	if ($_REQUEST['act'] != 'login' && !empty($_GET['username']) && !empty($_GET['usertoken']))
	{
		$str = substr(md5(AUTH_KEY), -8);
		$auth_str = encrypt($str, AUTH_KEY);
		$auth = encrypt($_GET['username'], AUTH_KEY).':'.$auth_str;

		wap_setcookie("WAP[auth]", $auth, -1);
		wap_setcookie("WAP[auth_token]", encrypt($_GET['usertoken'], AUTH_KEY), -1);

		define('FROM_CLIENT', 'APP');

		$mem->addCache('shop_user_info', array());
	}
	/* cookie 自动登录 */
	else if (empty( $user_token) && !empty($auth))
	{
		list($username, $auth_str) = explode(':', $auth);
		$username = decrypt($username, AUTH_KEY);
		$auth_str = decrypt($auth_str, AUTH_KEY);

		$password = wap_getcookie("WAP[auth$auth_str]");
		$password = decrypt($password, AUTH_KEY);

		$login = user_login($username, $password, false, false);
	}
}
